package com.example.project_speelveld;

import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

import com.model.speelveld.HighScore;
import com.model.speelveld.HighScoreAdapter;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;

// TODO: Auto-generated Javadoc
/**
 * The Class HighScoreActivity.
 */
public class HighScoreActivity extends Activity {
	
	/** The scores. */
	private ArrayList<HighScore> scores;
	
	/** The lv high score. */
	private ListView lvHighScore;
	
	/* (non-Javadoc)
	 * @see android.app.Activity#onCreate(android.os.Bundle)
	 */
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_high_score);
		getHighScores();
		Log.d("ScoresLijst", scores + "");
		//Intent i = getIntent();
		lvHighScore = (ListView) findViewById(R.id.highscoreview);
		if(scores != null)
		{
			lvHighScore.setAdapter(new HighScoreAdapter(lvHighScore.getContext() , 0, scores));
		}
		
	}
	
	/**
	 * Gets the high scores.
	 *
	 * @return the high scores
	 */
	public void getHighScores() {
		scores = new ArrayList<HighScore>();
		// Open the file
		FileInputStream fstream;
		try {
			fstream = new FileInputStream(getFilesDir() + "/HighScore.txt");
			// Get the object of DataInputStream
			DataInputStream in = new DataInputStream(fstream);
			BufferedReader br = new BufferedReader(new InputStreamReader(in));

			String strLine;
			String[] splits;

			while ((strLine = br.readLine()) != null) {
				splits = strLine.split(",");
				scores.add(new HighScore(splits[0], Integer.parseInt(splits[1])));
				//Log.d("HIGHSCORE", strLine);
			}
			in.close();

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		Collections.sort(scores);
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onCreateOptionsMenu(android.view.Menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.high_score, menu);
		return true;
	}

	/* (non-Javadoc)
	 * @see android.app.Activity#onBackPressed()
	 */
	public void onBackPressed() {
		Intent goToFirst = new Intent(this , MainMenu.class);
		goToFirst.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(goToFirst);
	}
	

}
